package FrameSwing;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.text.JTextComponent;

public class MyFrame extends JFrame
{

    private boolean action = true;
    private int frameX;
    private int frameY;
    private double angle;
    private double radius = 10;
    private int startX;
    private int startY;
    double SIN;
    double COS;
    private actionPanel aPanel;

    public MyFrame() {
        setTitle("Frame");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Toolkit t = Toolkit.getDefaultToolkit();
        Dimension d = t.getScreenSize();
        frameX = d.width / 2;
        frameY = d.height / 2;
        int windowWidth = 410;
        int windowHeight = 250;
        frameX = frameX - windowWidth / 2 + 100;
        frameY = frameY - windowHeight / 2;
        startX = d.width / 2;
        startY = d.height / 2;
        startX = startX - windowHeight / 2 + 200;
        startY = startY - windowWidth / 2 + 200;
        setBounds(frameX, frameY, windowWidth, windowHeight);
        aPanel = new actionPanel();
        add(aPanel);

    }

    public void runMainLoop() {
        while (true) {
            try {
                while (action) {
                    moveFrame(true);
                    Thread.sleep(200);
                }
                Thread.sleep(20);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void moveFrame(boolean left) {
        SIN = Math.sin(this.angle);
        COS = Math.cos(this.angle);
        if (left == true) {
            frameX = frameX - (int) (Math.sin(this.angle) * this.radius);
            frameY = frameY - (int) (Math.cos(this.angle) * this.radius);
            angle = angle + 0.1d;
        }
        else {
            frameX = frameX + (int) (Math.sin(this.angle) * this.radius);
            frameY = frameY + (int) (Math.cos(this.angle) * this.radius);
            angle = angle - 0.1d;
        }
        setLocation(frameX, frameY);
        aPanel.jl1.setText(String.valueOf(frameX));
        aPanel.jl2.setText(String.valueOf(frameY));
        aPanel.jl3.setText(String.valueOf(SIN));
        aPanel.jl4.setText(String.valueOf(COS));

        repaint();
    }

    class actionPanel extends JPanel
    {

        private Box mainBox;
        private JLabel jl1;
        private JLabel jl2;
        private JLabel jl3;
        private JLabel jl4;

        public actionPanel() {

            mainBox = Box.createVerticalBox();
            addButtons();
            add(mainBox);
            coordinatesPanel();
        }

        private void addButtons() {
            JPanel buttonPanel = new JPanel();
            JButton startButton = new JButton("Start");
            startButton.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    action = true;
                }
            });
            JButton pauseButton = new JButton("Pause");
            pauseButton.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    action = false;

                }
            });
            JButton stopButton = new JButton("Stop");
            stopButton.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    action = false;
                    MyFrame.this.setLocation(startX, startY);
                    frameX = startX;
                    frameY = startY;
                    angle = 0.00;
                    repaint();
                }
            });
            JButton leftButton = new JButton("Step Left");
            leftButton.addActionListener(new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent ae) {
                    moveFrame(true);

                }
            });
            JButton rightButton = new JButton("Step Right");
            rightButton.addActionListener(new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent ae) {
                    moveFrame(false);

                }
            });
            buttonPanel.add(leftButton);
            buttonPanel.add(startButton);
            buttonPanel.add(pauseButton);
            buttonPanel.add(stopButton);
            buttonPanel.add(rightButton);
            mainBox.add(buttonPanel);
        }

        private void getSinCos() {

        }

        private void coordinatesPanel() {

            jl1 = new JLabel(String.valueOf(frameX));
            jl2 = new JLabel(String.valueOf(frameY));

            mainBox.add(jl1);
            mainBox.add(jl2);

        }

    }

}